<?php
	class the_model extends the_model_object {			
		function name($name) {
			global $oh_object_model;
			// set name of last object created or called
			return $oh_object_model;
		}
		
		function meta($key,$value) {
			global $oh_object_model;
			// set name of last object created or called
			return $oh_object_model;
		}
		function get_where($args) {
			if ($args) {
				$w = array();
				foreach($args as $key=>$value) {
					if (false) {
					} else if ($key == "id") {
						$w[] = "$key = '$value'";				
					} else {
						$w[] = "$key LIKE '$value'";				
					}
				}
				$w = "WHERE ".implode(" AND ",$w);
			} else {
				$w = "";
			}
			return $w;
		}
		function get_set($data) {
			if ($data) {
				$s = array();
				foreach($data as $key=>$value) {
					$value = mysql_real_escape_string($value);
					$s[] = "$key = '$value'";
				}
				$s = "SET ".implode(", ",$s);					
			} else {
				$s = "";
			}				
			return $s;
		}	
		function get_class_insert($args) {
		}
		function get_insert($args) {
			$fields = array();
			$values = array();				
			if ($args->form) {
				$class = $this->get_class($args->form);
				foreach($args as $key=>$value) {
					$fields[] = $this->get_real_column($key,$class->form);
					$values[] = "'".$value."'";	
				}
				return array(
					"fields" => implode(",",$fields),
					"values" => implode(",",$values)
				);
			}
		}
		function get_object_insert($args) {
			$fields = array();
			$values = array();				
			if ($args->form) {
				$class = $this->get_class($args->form);
				foreach($args as $key=>$value) {
					$value = mysql_real_escape_string($value);
					//$real = $this->get_real_column($key,$class->form);					
					if (in_array($key,$this->model_object_fields["real"])) {
						$fields[] = $key;
						$values[] = "'".$value."'";	
					} else {
						$real = $class->fields['tyof'][$key];
						if (in_array($real,$this->model_object_fields["real"])) {
							$fields[] = $real;
							$values[] = "'".$value."'";	
						} else {
					
						}					
					}
				}
				return array(
					"fields" => implode(",",$fields),
					"values" => implode(",",$values)
				);
			}
		}
		function get_real_column($key,$def) {			
			if (isset($def['real'][$key])) {
				return $def['real'][$key];
			} else {
				return $key;
			}
		}
		function from($data) {
			global $oh_object_model;
			if ($this->selector) {
				$s = explode("::",$this->selector);
				$s['handle'] = strtolower(trim($s[0]));
				$s['query']  = trim($s[1]);
				if ($s['handle'] == "object") {				
					$o = $oh_object_model->{"get_".$s['handle']}($s['query']);				
					if (count($o)>0) {
						foreach($o as $item) {
							foreach($data as $key=>$value) {
								//$c = $this->get_class($item->form);								
								if (!in_array($key,$this->model_object_fields["real"])) {									
									$this->set_meta($item->id,$key,$value);									
								} else {
									//$this->set_object("id=".$item->id,(string)$c->form[$key],(string)$value);
								}
							}					
						}
					}
				}
			}
			$this->selector = NULL;
		}	
		function model_check($class,$key) {
			$prefix = $this->get_prefix();
			$q = "SELECT model FROM {$prefix}class WHERE name='$class' LIMIT 1";
			$m = oh($this->datasource)->get_results($q);
			$m = $this->model_parse($m[0]->model);			
		}
		function model_parse($model) {			
			$model = preg_replace("/[ \t\n\r]+/"," ",$model);
			return array();
		}
		function set_casting($args,$data) {
			if (isset($args->form)) {
				oh_dump($data);
				echo '!';
			} else if (isset($args->id)) {
				$o = $this->get_object("id=".$args->id);
				$o = $o[0];
				$c = $this->get_class($o->form);
				$d = array();
				if (count($data)>0) {
					foreach($data as $k=>$v) {
						$d[$c->form["full"][$k]] = $v;
					}				
				}
				return $d;
			}
		}
		function get_casting($A,$data) {
		}
		
		
		function set_decoration($r) {
			$i = 0;
			$c = count($r);
			if ($c > 0) {
				foreach($r as &$item) {
					$class = $this->get_class($item->form);

					foreach($class->fields['real'] as $key=>$value) {
						if ((strlen($key)>0)&&(strlen($value)>0)) {
							$item->{$value} = $item->{$key};							
						}
					}
					$item->info = new stdClass();
					$item->info->n = $i;
					if ($c == 1) {
						$item->info->single = true; 
					} else {
						$item->info->single = false; 
					}
					if (($i%2) == 1) {
						$item->info->odd = true; 
					} else {
						$item->info->odd = false; 
					}
					if ($i == 0) {
						$item->info->first = true; 
					} else {
						$item->info->first = false; 
					}
					$item->meta = $this->get_meta($item->id);
					$i++;
					if ($i == $c) {
						$item->info->last = true; 
					} else {
						$item->info->last = false; 
					}			
					if (count($class->fields["chil"])>0) {
						foreach($class->fields["chil"] as $type=>$name) {
							$item->$name = $this->get_object("$type&boss=".$item->id);
						}
					}
				}
			}
			return $r;
		}
		function test() {
			return $this->test_installation();
		}
		
		
	}
?>
